﻿using System;
using System.Collections.Generic;
using System.Data;
using visinia.SmartEngine.Cache;
using viWiki.Classes;
using viWiki.Providers;

namespace viWiki.Controllers
{
    public class NamespaceController
    {
        #region ACCESS
        /// <summary>
        /// Gets all the namespaces
        /// </summary>        
        public static List<NamespaceInfo> GetNamespaces()
        {
            List<NamespaceInfo> list = (List<NamespaceInfo>)DataCache.GetCache("WikiNamespaces");
            if (list == null)
            {
                list = FillNamespaces(viWikiDataProvider.Provider.GetNamespaces());
                DataCache.SetCache("WikiNamespaces", list);
            }
            return list;
        }

        /// <summary>
        /// Gets the namespace by its name
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static NamespaceInfo GetNamespace(string Name)
        {
            string name = Name.Trim();
            return GetNamespaces().Find(p => string.Compare(p.Name, name, true) == 0);
        }
                
        /// <summary>
        /// Gets the Namespace
        /// </summary>
        /// <param name="NamespaceInfoId"></param>
        /// <returns></returns>
        public static NamespaceInfo GetNamespace(int NamespaceInfoId)
        {
            return GetNamespaces().Find(p => p.NamespaceInfoId == NamespaceInfoId);
        }
        #endregion

        #region DATA
        /// <summary>
        /// Adds a new Namespace
        /// </summary>
        /// <param name="page">The Namespace to be added </param>
        public static int AddNamespace(NamespaceInfo nspace)
        {
            IDataReader dr = viWikiDataProvider.Provider.AddNamespace(nspace.Name, nspace.DefaultWikiPageId);
            int index = 0;
            while (dr.Read())
            {
                if (dr[0] != Convert.DBNull)
                    index = (int)dr[0];
            }
            dr.Close();
            DataCache.DeleteCacheMultiple("WikiNamespaces");
            return index;
        }

        /// <summary>
        /// Updates the WikiPage
        /// </summary>
        /// <param name="page"></param>
        public static void UpdateNamespace(NamespaceInfo nspace)
        {
            viWikiDataProvider.Provider.UpdateNamespace(nspace.NamespaceInfoId, nspace.Name, nspace.DefaultWikiPageId);
            DataCache.DeleteCacheMultiple("WikiNamespaces");
        }

        /// <summary>
        /// Deletes the Namespace
        /// </summary>
        /// <param name="NamespaceInfoId">Id of the Namespace to Delete</param>
        public static void DeleteNamespace(int NamespaceInfoId)
        {
            // delete namespaces wiki pages as well
            List<WikiPage> wikipages = WikiPageController.GetWikiPagesByNamespace(NamespaceInfoId);
            foreach (WikiPage wikipage in wikipages)
                WikiPageController.DeleteWikiPage(wikipage.WikiPageId);
            //delete namespace itself
            viWikiDataProvider.Provider.DeleteNamespace(NamespaceInfoId);
            DataCache.DeleteCacheMultiple("WikiNamespaces");
        }

        /// <summary>
        /// Deletes the Namespace
        /// </summary>
        /// <param name="page">The Namespace to Delete</param>
        public static void DeleteNamespace(NamespaceInfo nspace)
        {
            DeleteNamespace(nspace.NamespaceInfoId);
        }
        #endregion

        #region HELPERS
        private static NamespaceInfo FillNamespace(IDataReader dr)
        {
            while (dr.Read())
            {
                return ActualRead(dr, true);
            }
            return null;
        }
        private static List<NamespaceInfo> FillNamespaces(IDataReader dr)
        {
            List<NamespaceInfo> list = new List<NamespaceInfo>();
            while (dr.Read())
            {
                list.Add(ActualRead(dr, false));
            }
            dr.Close();
            return list;
        }
        private static NamespaceInfo ActualRead(IDataReader dr, bool CloseDataReader)
        {
            NamespaceInfo nspace = new NamespaceInfo();
            if (dr["NamespaceInfoId"] != Convert.DBNull)
                nspace.NamespaceInfoId = (int)dr["NamespaceInfoId"];
            if (dr["Name"] != Convert.DBNull)
                nspace.Name = (string)dr["Name"];
            if (dr["DefaultWikiPageId"] != Convert.DBNull)
                nspace.DefaultWikiPageId = (int)dr["DefaultWikiPageId"];            
            if (CloseDataReader)
                dr.Close();
            return nspace;
        }
        #endregion        
    }
}
